package com.bocai.movie.service;

import com.alibaba.fastjson.TypeReference;
import com.bocai.CacheKey;
import com.bocai.PageResult;
import com.bocai.config.RedisAdapter;
import com.bocai.movie.mapper.MovieMapper;
import com.bocai.movie.model.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author create by VIC.Huang
 * @date 2018/4/1
 */
@Service
public class MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private RedisAdapter redisAdapter;

    public PageResult<Movie> list(int page, int pageSize) {
        if (redisAdapter.exists(CacheKey.MOVIE_LIST + page)) {
            return redisAdapter.get(CacheKey.MOVIE_LIST + page, new TypeReference<PageResult<Movie>>() {
            });
        } else {
            PageResult<Movie> result = new PageResult<>(page, movieMapper.countMovie(), movieMapper.selectMovieList((page - 1) * pageSize, pageSize));

            redisAdapter.setex(CacheKey.MOVIE_LIST + page, 300, result);
            return result;
        }
    }
}
